<template>
  <el-form :model="form" ref="form" :rules="rules" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input placeholder="用户名/手机" v-model="form.username"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="captcha">
      <el-input placeholder="验证码" v-model="form.captcha">
        <template slot="append">
          <el-button @click="handleSendCaptcha">发送验证码</el-button>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item class="form-item" prop="nickname">
      <el-input placeholder="昵称" type="text" v-model="form.nickname"></el-input>
    </el-form-item>
    <el-form-item class="form-item" prop="password">
      <el-input placeholder="密码" type="password" v-model="form.password"></el-input>
    </el-form-item>
    <el-form-item class="form-item" prop="checkpass">
      <el-input placeholder="确认密码" type="password" v-model="form.checkpass"></el-input>
    </el-form-item>
    <el-button class="submit" type="primary" @click="handleRegSubmit">注册</el-button>
  </el-form>
</template>
<script>
export default {
  data() {
    //确认密码
    const validatePass = (rule, value, callback) => {
      // rule  校验规则
      // value  输入的密码
      // callback  校验完毕的放行函数，类似于next
      // 密码不能为空
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.form.password) {
        callback(new Error("两次输入的密码不一致！"));
      } else {
        callback();
      }
    };
    return {
      form: {
        username: "",
        captcha: "",
        nickname: "",
        password: "",
        checkpass: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          { min: 11, message: "手机格式错误", trigger: "blur" }
        ],
        captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 10, message: "长度在 6 到 10 个字符", trigger: "blur" }
        ],
        checkpass: [{ validator: validatePass, trigger: "blur" }]
      }
    };
  },
  methods: {
    handleSendCaptcha() {
      //手机号码不能为空
      if (!this.form.username) {
        this.$confirm("手机号码不能为空", "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning"
        });
        return;
      }

      if (this.form.username.length !== 11) {
        this.$confirm("手机号码格式错误", "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "warning"
        });
        return;
      }

      this.$axios({
        url: `captchas`,
        method: "POST",
        data: {
          tel: this.form.username
        }
      }).then(res => {
        const { code } = res.data;
        this.$confirm(`模拟手机验证码为：${code}`, "提示", {
          confirmButtonText: "确定",
          showCancelButton: false,
          type: "success"
        });
      });
    },
    handleRegSubmit() {
      console.log("form表单数据", this.form);
      // 提取需要的数据
      //  方法1
      const { checkpass, ...props } = this.form;
      console.log("props", props);
      // 发送之前先验证form表单数据
      this.$refs.form.validate(isValid => {
        if (isValid) {
          this.$store.dispatch("user/register", props).then(res => {
            console.log("注册后返回的res", res);
            if (res.status == 200) {
              this.$message({
                message: "注册成功，页面即将跳转",
                type: "success"
              });
            }
            //页面跳转到登录页面
            setTimeout(() => {
              this.$emit("jump2Login", 0);
            }, 1000);
          });
        }
      });
    }
  }
};
</script>
<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>
